<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use App\Admin\Repositories\Config;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserController extends AdminController
{
    protected $translation = 'user';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    public function grid($type = null)
    {
        return Grid::make(new User('shop'), function (Grid $grid) use ($type) {
            
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id');
            $grid->column('name')->display(function ($value) {
                return '<a href="'.admin_url('home?user_id='.$this->id).'" target="_blank">'.$value.'</a>';
            });
            $grid->column('mobile');
            $grid->column('class_name')->display(function ($value) {
                return Config::getName($value, 'gradeOptions');
            });
            $grid->column('sex')->display(function ($value) {
                return Config::getName($value, 'sexOptions');
            });
            $grid->column('birthdate');
            if(!\Dcat\Admin\Admin::user()->isAdministrator()) {
                $grid->model()->where('shop_id', session('shop_id'));
            }else{
                $grid->column('shop.name', '门店');
            }
            $grid->column('created_at');

            if($type == -1) {
                $grid->disableToolbar();
                $grid->disableActions();
                // $grid->column('ids', '操作')->display(function(){
                //     return "档案 病例";
                // });
            }
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                // append一个操作
                $actions->append('<a href="'.admin_url('home?user_id='.$this->id).'" target="_blank"><i class="fa fa-eye"></i></a>');
                $actions->disableView();
            });
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('name')->width(3);
                $filter->like('mobile')->width(3);
                if(\Dcat\Admin\Admin::user()->isAdministrator()) {
                    $filter->equal('shop_id', '门店')->select(\App\Models\Shop::where('status', 1)->pluck('name', 'id'))->width(3);
                }
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->row(function (Show\Row $show) {
                $show->width(3)->id;
                $show->width(3)->name;
                $show->width(5)->mobile;
            });
            $show->row(function (Show\Row $show) {
                $show->width(3)->class_name;
                $show->width(3)->address;
                $show->width(5)->sex->as(function ($value) { 
                    return Config::getName($value, 'sexOptions');
                 });
            });
             $show->row(function (Show\Row $show) {
                $show->width(3)->school;
                $show->width(3)->birthdate;
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    public function form($id = null)
    {
        return Form::make(new User(), function (Form $form) use ($id) {
            if($id == -1) {
                $form->disableFooter();
                $form->disableHeader();
            }
            // $form->select('mobile')->options(function($){
            //     $user = User::find($form);
            //     if ($user) {
            //         return [$user->id => $user->name];
            //     }
            // })->ajax(admin_url('api/user'))->config('tags', true)->required();
            // $form->display('id');
            $js = <<<JS
    function (suggestion) {
        $.each(suggestion.data, function(i,d){
            if(i == 'class_name' || i=='sex'){
                $('#tab_user-form-tab form input[name="'+i+'"]').eq(d).trigger('click');
            }else{
                $('#tab_user-form-tab form .field_'+i).val(d)
            }
            
        })
    }
JS;
            $form->autocomplete('mobile')->ajax(admin_url('api/userAutocomplete'),  'text', 'region')->configs([
                
                'onSelect' => \Dcat\Admin\Support\JavaScript::make($js),
            ])->required();
            $form->text('name')->required();
            
            $form->text('school')->required();
            $form->radio('class_name')->options(Config::$gradeOptions)->required();
            $form->radio('sex', '性别')->options(Config::$sexOptions)->required();
            $form->date('birthdate');
            $form->text('address');
            $form->hidden('shop_id')->default(session('shop_id'));
        });
    }
}
